package dk.flashnotes.fffExample {
	import dk.flashnotes.fff.FFFBase;
	import dk.flashnotes.fff.FFFBaseEvent;
	import dk.flashnotes.fff.logger.FFFLogger;
	import dk.flashnotes.fff.logger.ThunderBoltLogRouter;
	import dk.flashnotes.fffExample.fff.CustomElementFactory;
	import dk.flashnotes.fffExample.pages.HomePage;
	import dk.flashnotes.fffExample.pages.Popup;
	import dk.flashnotes.fffExample.parts.DefaultTitlePart;
	import dk.flashnotes.fffExample.parts.Footer;
	import dk.flashnotes.fffExample.parts.ParameterList;
	
	import org.osflash.thunderbolt.Logger;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.text.Font;		

	/**
	 * @author Josef Kj�rgaard
	 */
	public class SiteRoot extends Sprite {

		private var _oFFFBase : FFFBase;
		private var _oFontLoader : Loader;
		private var _sprGADebug : Sprite;
		private var _sprFFFHolder : Sprite;

		public function SiteRoot() {
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			loadFonts();			
		}
		
		private function loadFonts() : void {
			_oFontLoader = new Loader();
			_oFontLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onFontsLoaded, false, 0, true);
			_oFontLoader.load(new URLRequest("swf_assets/fontLib.swf"));
		}

		private function onFontsLoaded(e : Event) : void {
			setupFFF();
		}

		private function setupFFF() : void {
			
			_sprFFFHolder = new Sprite();
			addChild(_sprFFFHolder);
			
			FFFLogger.oFFFLogRouter = new ThunderBoltLogRouter();
				 
			_oFFFBase = FFFBase.get_oInstance();
			_oFFFBase.register_cAPageTypes(HomePage, Popup, BasicSub);
			_oFFFBase.register_cAPartTypes(DefaultTitlePart, Footer, ParameterList);
			
			_oFFFBase.loadAndInitFFFTree(this, true, "xml_data/full_data_parameter_test.xml", new CustomElementFactory());
			_oFFFBase.addEventListener(FFFBaseEvent.etTREE_CREATED, onTreeCreated, false, 0, true);
			
			_oFFFBase.bTracingEnabled=false;
		}

		private function onTreeCreated(e : Event) : void {
			
			_sprGADebug = new Sprite();
			addChildAt(_sprGADebug,0);
			
			_oFFFBase.addEventListener(FFFBaseEvent.GOOGLE_ANALYTICS_BRIDGE_READY, onGAReady, false, 0, true);
			_oFFFBase.loadAndInitGoogleAnalyticsModule('ga_module/', 'basic_test');
		}
		
		private function onGAReady(event : FFFBaseEvent) : void {
			_oFFFBase.startTreeControl();
			setChildIndex(_sprGADebug, 0);
		}
	}
}
